package com.quad.innovators.salesease.controller.task;

import com.quad.innovators.salesease.annotation.DataPermission;
import com.quad.innovators.salesease.annotation.FunctionPermission;
import com.quad.innovators.salesease.annotation.Log;
import com.quad.innovators.salesease.annotation.RepeatSubmit;
import com.quad.innovators.salesease.common.result.PageResultBean;
import com.quad.innovators.salesease.common.result.Result;
import com.quad.innovators.salesease.constants.SystemConstants;
import com.quad.innovators.salesease.enums.LogModuleEnum;
import com.quad.innovators.salesease.model.form.task.TaskDistributionForm;
import com.quad.innovators.salesease.model.query.task.TaskDistributionQuery;
import com.quad.innovators.salesease.model.vo.task.TaskDistributionVO;
import com.quad.innovators.salesease.service.task.TaskDistributionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author cheng
 */
@Tag(name = "10.任务发布接口")
@RestController
@RequestMapping(SystemConstants.BACKEND_API + "/task-distribution")
@RequiredArgsConstructor
public class TaskDistributionController {

    private final TaskDistributionService taskDistributionService;

    @Operation(summary = "分页列表")
    @GetMapping("/page")
    @Log(value = "分页列表", module = LogModuleEnum.TASK_DISTRIBUTION)
    @DataPermission(permissionCode = "task:task-distribution:data-permission")
    public Result<PageResultBean<TaskDistributionVO>> listPaged(TaskDistributionQuery queryParams) {
        PageResultBean<TaskDistributionVO> result = taskDistributionService.listPaged(queryParams);
        return Result.success(result);
    }

    @Operation(summary = "新增")
    @PostMapping
    @FunctionPermission(permissionCode = "task:task-distribution:add")
    @RepeatSubmit
    public Result<?> save(@RequestBody @Valid TaskDistributionForm form) {
        return Result.success(taskDistributionService.save(form));
    }

    @Operation(summary = "表单数据")
    @GetMapping("/{code}/form")
    @Parameters({
            @Parameter(name = "code", description = "code"),
    })
    @FunctionPermission(permissionCode = "task:task-distribution:detail")
    @DataPermission(permissionCode = "task:task-distribution:data-permission")
    public Result<TaskDistributionForm> getForm(@PathVariable("code") String code) {
        return Result.success(taskDistributionService.getFormData(code));
    }

    @Operation(summary = "修改")
    @PutMapping(value = "/{code}")
    @Parameters({
            @Parameter(name = "code", description = "code")
    })
    @FunctionPermission(permissionCode = "task:task-distribution:edit")
    @DataPermission(permissionCode = "task:task-distribution:data-permission")
    public Result<?> update(@PathVariable("code") String code, @RequestBody @Validated TaskDistributionForm form) {
        return Result.success(taskDistributionService.update(code, form));
    }

    @Operation(summary = "修改状态")
    @PutMapping(value = "/{code}/status")
    @FunctionPermission(permissionCode = "task:task-distribution:status")
    @DataPermission(permissionCode = "task:task-distribution:data-permission")
    public Result<Void> updateStatus(@PathVariable("code") String code, @RequestParam("status") Integer status, @RequestParam(value = "addressType", required = false) Integer addressType, @RequestParam(value = "addressCode",required = false) String addressCode) {
        boolean result = taskDistributionService.updateStatus(code, status, addressType, addressCode);
        return Result.judge(result);
    }

}
